/*
 * @(#)RecallRate.java
 *
 * Copyright 2011 Xinhua Online, Inc. All rights reserved.
 */

package search;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

/**
 * description 召回率计算
 * @author  huangyixiang
 * @version 2012-1-5
 */
public class RecallRate {
	public static final String WORD_FILE = "d:/召回率统计.txt";
	
	public static String verbatimSplitServer = "http://10.1.2.46:8099/search/select/?version=2.2&start=0&rows=0&debugQuery=off&defType=extlucene";
	public static String ikSplitServer = "http://10.1.2.46:8004/search/select/?version=2.2&start=0&rows=0&debugQuery=off&defType=extlucene";
	
	private static String getVerbatimQuery(String q){
		try {
			return verbatimSplitServer + "&q=" + URLEncoder.encode(q,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	private static String getIKQuery(String q){
		try {
			return ikSplitServer + "&q=" + URLEncoder.encode(q,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	private static List<String> readtxt() throws IOException {
		List<String> words  = new ArrayList<String>();
		BufferedReader br = new BufferedReader(new FileReader(WORD_FILE));
		String r = br.readLine();
		while (r != null) {
			if(r == null || r.trim().equals("")){
				continue;
			}
			words.add(r.trim());
			r = br.readLine();
		}
		br.close();
		return words;
	}
	
	private static int request(String url) throws ParserException{
		Parser myParser = new Parser(url);
		myParser.setEncoding("UTF-8");
		TagNameFilter filter = new TagNameFilter("result");
		HasAttributeFilter attFilter = new HasAttributeFilter("numFound");
		AndFilter numFilter = new AndFilter(filter,attFilter);
		
		NodeList contentNodeList = myParser.extractAllNodesThatMatch(numFilter);
		TagNode tg = (TagNode)contentNodeList.elementAt(0);
		return Integer.parseInt(tg.getAttribute("numFound"));
	}
	
	private static int requestVerbatim(String q) throws ParserException{
		return request(getVerbatimQuery(q));
	}
	
	private static int requestIK(String q) throws ParserException{
		return request(getIKQuery(q));
	}
	
	private static void calculateAndSave() throws Exception{
		List<String> words = readtxt();
		List<WordNum> list = new ArrayList<WordNum>();
		int verbatimNum;
		int ikNum;
		WordNum wordNum;
		for(String word :  words){
			ikNum = requestIK(word);
			verbatimNum = requestVerbatim(word);
			wordNum = new WordNum();
			wordNum.setWord(word);
			wordNum.setIkNum(ikNum);
			wordNum.setVerbatimNum(verbatimNum);
			float recallRate = (float)ikNum/verbatimNum;
			if(recallRate > 1){
				recallRate = 1;
			}
			wordNum.setReCallRate(recallRate);
			list.add(wordNum);
		}
		saveToFile(list);
	}
	
	private static void saveToFile(List<WordNum> list) throws IOException{
		File file = new File(WORD_FILE);
		file.createNewFile();
		BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
		
		float totalRate = 0;
		for(WordNum wordNum : list){
			bufferedWriter.write(wordNum.getWord() + "\t"
					+ wordNum.getIkNum() + "\t"
					+ wordNum.getVerbatimNum() + "\t"
					+ wordNum.getReCallRate() + "\r\n");
			totalRate += wordNum.getReCallRate();
		}
		float averageRate = totalRate / list.size();
		bufferedWriter.write("\r\n平均召回率 : " + averageRate);
		bufferedWriter.flush();
		bufferedWriter.close();
	}
	
	private static class WordNum{
		private String word;
		private int ikNum;
		private int verbatimNum;
		private float reCallRate;
		public String getWord() {
			return word;
		}
		public void setWord(String word) {
			this.word = word;
		}
		public int getIkNum() {
			return ikNum;
		}
		public void setIkNum(int ikNum) {
			this.ikNum = ikNum;
		}
		public int getVerbatimNum() {
			return verbatimNum;
		}
		public void setVerbatimNum(int verbatimNum) {
			this.verbatimNum = verbatimNum;
		}
		public float getReCallRate() {
			return reCallRate;
		}
		public void setReCallRate(float reCallRate) {
			this.reCallRate = reCallRate;
		}
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println("---------------");
		calculateAndSave();
	}

}
